package com.googlecode.pdfbricks.cli;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.googlecode.pdfbricks.cli.constant.OptionConstant;
import com.googlecode.pdfbricks.cli.parser.MergeCommandParser;
import com.googlecode.pdfbricks.cli.parser.SplitCommandParser;
import com.googlecode.pdfbricks.engine.base.Executable;

/**
 * @author Johny Huang
 *
 */
public class CommandConsole {

	public static void main(String[] args) {
		String temp = null;
		do {
			BufferedReader in = new BufferedReader(new InputStreamReader(
					System.in));
			System.out.print("PDFBricks>");
			try {
				temp = in.readLine();
				String[] params=temp.split(" ");
				
				Options opts=new Options();
				opts.addOption(OptionConstant.MERGE);
				opts.addOption(OptionConstant.SPLIT);
				opts.addOption(OptionConstant.TO);
				opts.addOption(OptionConstant.FROM);
				CommandLineParser parser=new GnuParser();
				CommandLine cl = parser.parse(opts,params);
				
				if(cl.hasOption(OptionConstant.MERGE.getOpt())){
					System.out.println("Executing a merging command...");
				    Executable exe=MergeCommandParser.getInstance().parse(cl);
				    exe.execute();
				}
				if(cl.hasOption(OptionConstant.SPLIT.getOpt())){
					System.out.println("Executing a splitting command...");
					Executable exe=SplitCommandParser.getInstance().parse(cl);
					exe.execute();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}catch (ParseException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		} while (!"exit".equalsIgnoreCase(temp));
	}
}
